/*
 * io.c
 *
 * Created: 8/22/2013 1:11:58 PM
 *  Author: Ken Arok
 *
 * \file
 *
 * \brief Input Output Drivers
 *
 * Copyright (c) 2013 PT Hanindo Automation Solutions. All rights reserved.
 *
 */

#include <string.h>
#include "uart.h"
#include "spi.h"
#include "uart_io.h"
#include "spi_io.h"


/* Base address of ROM Block 1. */
void *ROM_Base_Block1 = NULL;


void vIO_Init(void)
{
	/* Initialize UART. */
	uart_io_init();
	
	/* Initialize SPI. */
	spi_io_init();
}


portBASE_TYPE vIO_Ctrl(io_descriptor_t io_desc, io_request_code_t reqCode, void *pValue)
{
	const char spi_path[] = "SPI";
	const char uart_path[] = "COM";
	spi_peripheral_descriptors_t *_spi_desc = io_desc;
	uart_peripheral_descriptors_t *_uart_desc = io_desc;
	portBASE_TYPE _retError = pdFAIL;
	
	/* Check if it is SPI peripheral. */
	if(!strncmp(_spi_desc->path_name, spi_path, 4)) {
		_retError = spi_io_ctrl(io_desc, reqCode, pValue);
	}
	else if(!strncmp(_uart_desc->path_name, uart_path, 4)) {
		_retError = uart_io_ctrl(io_desc, reqCode, pValue);
	}
	
	return _retError;
}


io_descriptor_t vIO_Open(IO_path path_name, const uint8_t ioFlag)
{
	const char spi_path[] = "SPI";
	const char uart_path[] = "COM";
	
	if(!memcmp((const void *)path_name, (const void *)spi_path, 3)) {
		return spi_io_open(path_name, ioFlag);
	}
	else if(!memcmp((const void *)path_name, (const void *)uart_path, 3)) {
		return uart_io_open(path_name, ioFlag);
	}

	return NULL;
}


portBASE_TYPE vIO_Close(io_descriptor_t io_desc, const uint8_t ioFlag)
{
	const char spi_path[] = "SPI";
	const char uart_path[] = "COM";
	spi_peripheral_descriptors_t *_spi_desc = io_desc;
	uart_peripheral_descriptors_t *_uart_desc = io_desc;
	portBASE_TYPE _retError = pdFAIL;
	
	if(!strncmp(_spi_desc->path_name, spi_path, 4)) {
		_retError = spi_io_close(io_desc, ioFlag);
	}
	else if(!strncmp(_uart_desc->path_name, uart_path, 4)) {
		_retError = uart_io_close(io_desc, ioFlag);
	}

	return _retError;
}


portBASE_TYPE vIO_Write(io_descriptor_t io_desc, const void *pBuf, uint32_t size, io_rw_flag_t flag)
{
	const char spi_path[] = "SPI";
	const char uart_path[] = "COM";
	spi_peripheral_descriptors_t *_spi_desc = io_desc;
	uart_peripheral_descriptors_t *_uart_desc = io_desc;
	portBASE_TYPE _retError = pdFAIL;
	
	if(!strncmp(_spi_desc->path_name, spi_path, 4)) {
		_retError = spi_io_write(io_desc, pBuf, size, flag);
	}
	else if(!strncmp(_uart_desc->path_name, uart_path, 4)) {
		_retError = uart_io_write(io_desc, pBuf, size, flag);
	}

	return _retError;
}


portBASE_TYPE vIO_Read(io_descriptor_t io_desc, void *pBuf, uint32_t size, io_rw_flag_t flag)
{
	const char spi_path[] = "SPI";
	const char uart_path[] = "COM";
	spi_peripheral_descriptors_t *_spi_desc = io_desc;
	uart_peripheral_descriptors_t *_uart_desc = io_desc;
	portBASE_TYPE _retError = pdFAIL;
	
	if(!strncmp(_spi_desc->path_name, spi_path, 4)) {
		_retError = spi_io_read(io_desc, pBuf, size, flag);
	}
	else if(!strncmp(_uart_desc->path_name, uart_path, 4)) {
		_retError = uart_io_read(io_desc, pBuf, size, flag);
	}
	
	return _retError;
}
